package com.jinshan.oss.controller;

import com.jinshan.oss.service.OssService;
import com.jinshan.response.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;

@RestController
@RequestMapping("/oss")
@CrossOrigin
public class OssController {
    @Autowired
    private OssService ossService;

    @PostMapping("/uploadFile")
    public RetVal uploadFile(MultipartFile file) throws Exception {
        System.out.println(file);
        String retUrl = ossService.uploadFile(file);
        if ("".equals(retUrl)) {
            return RetVal.error().message("上传文件失败");
        } else {
            return RetVal.success().message("上传文件成功").data("retUrl", retUrl);
        }
    }

    @DeleteMapping("/delete")
    public RetVal deleteFile(String fileName) {
        System.out.println("===================fileName " + fileName);
        boolean flag = ossService.deleteFile(fileName);
        if (flag) {
            return RetVal.success().message("删除文件成功");
        } else {
            return RetVal.error().message("删除文件失败");
        }
    }

    @DeleteMapping("/batchDelete")
    public RetVal batchDeleteFile(String... files) {
        boolean flag = ossService.batchDeleteFile(files);
        if (flag) {
            return RetVal.success().message("批量删除文件成功");
        } else {
            return RetVal.error().message("批量删除文件失败");
        }
    }
}
